﻿using System;
using System.Collections.Generic;
using System.Text;

namespace 单例模式
{
    class Singleton2
    {
        //普通单例模式 需要写锁来防止多线程派生
        private static Singleton2 singleton2;
        private static Object o = new object();

        private string _name;

        private Singleton2() { }

        public string Name { get => _name; set => _name = value; }

        public static Singleton2 GetInstance()
        {
            if (singleton2 == null)  //双检锁
            {
                lock (o)
                {
                    if (singleton2 == null)
                    {
                        singleton2 = new Singleton2();
                    }
                }
            }
            return singleton2;
        }
    }
}
